package com.mf.ergate.controller.opt;

import com.mf.ergate.api.opt.OrgInfoRemoteService;
import com.mf.ergate.api.opt.model.dto.OrgInfoCreateDto;
import com.mf.ergate.api.opt.model.dto.OrgInfoDeleteDto;
import com.mf.ergate.api.opt.model.dto.OrgInfoQueryDto;
import com.mf.ergate.api.opt.model.dto.OrgInfoUpdateDto;
import com.mf.ergate.api.opt.model.vo.OrgInfoVo;
import com.mf.ergate.common.utils.MfBeanUtils;
import com.mf.ergate.domain.model.opt.OrgInfoEntity;
import com.mf.ergate.service.opt.OrgInfoService;
import com.mf.framework.common.model.Page;
import com.mf.framework.common.model.RemotePage;
import com.mf.framework.common.model.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 机构信息Controller
 * User: zhaoming
 * DateTime: 2019-04-16
 * To change this template use File | Settings | File Templates.
 */
@RestController
public class OrgInfoController implements OrgInfoRemoteService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private OrgInfoService orgInfoService;

    /**
     * 新增
     * @param orgInfoCreateDto 机构信息新增Dto
     * @return 机构信息
     */
    public Result<OrgInfoVo> insert(@RequestBody OrgInfoCreateDto orgInfoCreateDto) {
        OrgInfoEntity orgInfoEntity = MfBeanUtils.copyObject(orgInfoCreateDto, OrgInfoEntity.class);
        orgInfoEntity = orgInfoService.insert(orgInfoEntity);
        OrgInfoVo orgInfoVo = MfBeanUtils.copyObject(orgInfoEntity, OrgInfoVo.class);
        return new Result<>(orgInfoVo);
    }

    /**
     * 根据唯一编号更新
     * @param orgInfoUpdateDto 机构信息更新Dto
     * @return 执行结果
     */
    public Result<Boolean> updateByUniqueNo(@RequestBody OrgInfoUpdateDto orgInfoUpdateDto) {
        OrgInfoEntity orgInfoEntity = MfBeanUtils.copyObject(orgInfoUpdateDto, OrgInfoEntity.class);
        Boolean flag = orgInfoService.updateByUniqueNo(orgInfoEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号删除(逻辑删除)
     * @param orgInfoDeleteDto 机构信息删除Dto
     * @return 结果集
     */
    public Result<Boolean> deleteByUniqueNo(@RequestBody OrgInfoDeleteDto orgInfoDeleteDto){
        OrgInfoEntity orgInfoEntity = MfBeanUtils.copyObject(orgInfoDeleteDto, OrgInfoEntity.class);
        Boolean flag = orgInfoService.deleteByUniqueNo(orgInfoEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号查询
     * @param uniqueNo 唯一编号
     * @return 机构信息
     */
    public Result<OrgInfoVo> selectByUniqueNo(@RequestBody String uniqueNo) {
        OrgInfoEntity orgInfoEntity = orgInfoService.selectByUniqueNo(uniqueNo);
        OrgInfoVo orgInfoVo = MfBeanUtils.copyObject(orgInfoEntity, OrgInfoVo.class);
        return new Result<>(orgInfoVo);
    }

    /**
     * 根据条件查询
     * @param orgInfoQueryDto 机构信息查询Dto
     * @return 机构信息列表
     */
    public Result<List<OrgInfoVo>> selectByCondition(@RequestBody OrgInfoQueryDto orgInfoQueryDto) {
        List<OrgInfoEntity> orgInfoEntities = orgInfoService.selectByCondition(orgInfoQueryDto);
        List<OrgInfoVo> orgInfoVos = MfBeanUtils.copyList(orgInfoEntities, OrgInfoVo.class);
        return new Result<>(orgInfoVos);
    }

    /**
     * 根据条件查询数量
     * @param orgInfoQueryDto 机构信息查询Dto
     * @return 数量
     */
    public Result<Integer> selectCount(@RequestBody OrgInfoQueryDto orgInfoQueryDto) {
        return new Result<>(orgInfoService.selectCount(orgInfoQueryDto));
    }

    /**
     * 根据条件分页查询
     * @param remotePage 远程分页对象
     * @return 机构信息分页列表
     */
    public Result<Page<OrgInfoVo>> selectPage(@RequestBody RemotePage<OrgInfoQueryDto> remotePage){
        Page<OrgInfoEntity> orgInfoEntityPage = orgInfoService.selectPage(remotePage.getPage(), remotePage.getObj());
        Page<OrgInfoVo> page = MfBeanUtils.copyPage(orgInfoEntityPage, OrgInfoVo.class);
        return new Result<>(page);
    }

}